<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    let num = 10000;
    let randomNum = Math.ceil(Math.random()*num);
    document.title = randomNum;
    let arr = [];
    for (let i = 1; i <= num; i++) {
      arr.push(i);
    }
    console.log(arr);
    // 二分查找法适合有序 有序的数组
    function show(arr, randomNum) {
      let first = 0;
      let last = arr.length - 1;
      while(first <= last) {
        let mIndex = Math.floor(first + last) / 2;
        if (randomNum < arr[mIndex]) {
          last = mIndex - 1;
        } else if (randomNum > arr[mIndex]) {
          first = mIndex + 1;
        } else {
          return arr[mIndex];
        }
      }
    }

    console.log(show(arr, randomNum));
  </script>
</body>
</html>